/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.opensource.jdom.taggen.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 *
 * @author sergio.valdez
 */
public class SettingsUtil {

    public static final String POINT = ".";
    public static final String SLASH = "/";
    public static final String FROM  = "from";
    public static final String TO    = "to";
    public static final String FILE_NAME = "taggen.properties";
    public static final String ATT_ATTRIBUTES_COUNT
            = "att.take.attributes.form.counter";
    public static final String ATT_TAKE_ATTR_FROM = "att.take.attributes.form.";
    public static final String TAG_LOWERCASED     = "tag.lowercased";
    public static final String ATT_LOWERCASED     = "att.lowercased";
    public static final String TAG_REP_LOWGUI_FROM 
            = "tag.replace.lowguide.from";
    public static final String TAG_REP_LOWGUI_TO  = "tag.replace.lowguide.to";
    public static final String ATT_REP_LOWGUI_FROM
            = "att.replace.lowguide.from";
    public static final String ATT_REP_LOWGUI_TO   = "att.replace.lowguide.to";
    public static final String ATT_REP_PREFIX_FROM = "att.replace.prefix.from";
    public static final String ATT_REP_PREFIX_TO   = "att.replace.prefix.to";


    private static Map<String, Properties> props;
    public static Map<String, Settings> settings;

    static {
        props = new HashMap<String, Properties>(3);
        settings = new HashMap<String, Settings>(5);
    }

    public static void createDefaultProps(String strPackage) throws IOException {

        assert props == null : "Props Map Should never be null.";

        if (props != null
                && !props.containsKey(strPackage))
        {
            InputStream in = SettingsUtil.class.getResourceAsStream(
                    ("/"+strPackage.replace(POINT, SLASH) + SLASH + FILE_NAME)
                    .replace("package ", ""));

            props.put(strPackage, new Properties());

            if (in != null) props.get(strPackage).load(in);

        }
    }

    private static String getPropertie(String fromPackage, String key) throws
            IOException {
        return props.get(fromPackage).getProperty(key, "");
    }

    public static Settings createSettings(String fromPackage) throws IOException,
            ClassNotFoundException{
        Settings set = null;

        assert settings == null : "Settings Map Should never be null.";

        if (settings != null && !settings.containsKey(fromPackage)) {
            set = new Settings();
            int classCount =
                    getPropertie(fromPackage, ATT_ATTRIBUTES_COUNT).equals("")
                    ? 0
                    : Integer.parseInt(getPropertie(fromPackage,
                                                    ATT_ATTRIBUTES_COUNT));

            Class[] clazz = new Class[classCount];

            for (int i = 0; i < classCount; i++) {
                
                clazz[i] = Class.forName(getPropertie(fromPackage, 
                                                ATT_TAKE_ATTR_FROM + (i+1)));
            }

            set.setAttFromClasess(clazz);
            set.setAttLowercased(
                    getPropertie(fromPackage, ATT_LOWERCASED)
                    .equalsIgnoreCase("true"));
            set.setTagReplaceLowguideFrom(getPropertie(fromPackage,
                    TAG_REP_LOWGUI_FROM));
            set.setTagReplaceLowguideTo(getPropertie(fromPackage,
                    TAG_REP_LOWGUI_TO));
            set.setTagLowercased(getPropertie(fromPackage, TAG_LOWERCASED)
                    .equalsIgnoreCase("true"));
            set.setAttReplaceLowguideFrom(getPropertie(fromPackage, 
                    ATT_REP_LOWGUI_FROM));
            set.setAttReplaceLowguideTo(getPropertie(fromPackage, 
                    ATT_REP_LOWGUI_TO));
            set.setAttReplacePrefixFrom(getPropertie(fromPackage, 
                    ATT_REP_PREFIX_FROM));
            set.setAttReplacePrefixTo(getPropertie(fromPackage,
                    ATT_REP_PREFIX_TO));

        } else {
            set = settings.get(fromPackage);
        }
        
        return set;
    }

}
